package com.zzq.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzq.constant.MessageConstant;
import com.zzq.constant.RedisMessageConstant;
import com.zzq.entity.Result;
import com.zzq.service.OrderService;
import com.zzq.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

/**
 * 幼儿猿大班
 * 2021/7/27
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/saveOrder")
    public Result saveOrder(@RequestBody Map map){
        Result result =null;
        try {
        Jedis jedis = jedisPool.getResource();
        String validate = jedis.get((String) map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
        if(!validate.equals(map.get("validateCode"))){
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
            result = orderService.saveOrder(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Map<String,Object> map=orderService.findById(id);
            Date orderDate = (Date) map.get("orderDate");
            String s = DateUtils.parseDate2String(orderDate);
            map.put("orderDate",s);
            return new Result(true,null,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null);
        }

    }
}
